﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotnet.Utils.Utility.Http
{
    public static class UrIUtil
    {
        /*
          Uri.SchemeDelimiter="://";
          Uri.UriSchemeHttp="http";
          Uri.UriSchemeHttp="https"
          Uri.UriSchemeHttp + Uri.SchemeDelimiter="http://"         
         */


        /// <summary>
        /// uri地址合并
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="paths"></param>
        /// <returns></returns>
        public static Uri Combine(this Uri uri, params string[] paths)
        {
            //使用：//var url = new Uri("http://example.com/subpath/").Combine("/part1/", "part2").AbsoluteUri;
            return new Uri(paths.Aggregate(uri.AbsoluteUri, (current, path) => string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/'))));
        }

        /// <summary>
        /// uri地址合并
        /// </summary>
        /// <param name="segments"></param>
        /// <returns></returns>
        public static string Combine(params string[] segments)
        {
            return segments.Aggregate((current, segment) => $"{current.TrimEnd('/')}/{segment.TrimStart('/')}");
        }
    }
}
